<?php
//by willson 105328468@qq.com 2014/10/11
//index page interface

class cron extends core {
	public function __construct() {
		parent::__construct();
	}

	//取消24小时内没有支付的订单
	public function order_cancel() {
		$now = time();
		$hours_24 = $now - 24 * 3600;
		$sql = "UPDATE jf_order SET status='-1', memo='系统自动取消订单', cancel_time='{$now}' WHERE status='0' AND paid_time='0' AND add_time<'{$hours_24}'";
		$this->db->query($sql);
		$log = fopen(ROOT_PATH."/temp/cron.log", 'a+');
		fwrite($log, date("Y-m-d H:i:s")." order_auto_cancel \n\r");
		fclose($log);
	}

	//已经支付，并且超过服务时间1个小时的订单，默认为已经完成、等待评价
	public function order_finish() {
		$now = time();
		$one_hour = $now - 1 * 3600;
		$one_hour = date("Y-m-d h:i:s", $one_hour);
		$sql = "UPDATE jf_order SET status='4', memo='系统自动确认订单', end_time='{$now}' WHERE status IN (1, 2, 3) AND service_end_time<>'' AND service_end_time<'{$one_hour}'";
		//echo $sql;
		//exit;
		$this->db->query($sql);
		$log = fopen(ROOT_PATH."/temp/cron.log", 'a+');
		fwrite($log, date("Y-m-d H:i:s")." order_auto_finish \n\r");
		fclose($log);
	}

	//已经完成的订单，默认一天后好评，5星
	public function order_comment() {
		$after_one_day = date("Y-m-d H:i", time() + 24*3600);
		$sql = "SELECT * FROM jf_order WHERE status='4' AND service_end_time<'{$after_one_day}'";
		//echo $sql."<br>";
		$orders = $this->db->get_results($sql);
		$log = fopen(ROOT_PATH."/temp/cron.log", 'a+');
		fwrite($log, date("Y-m-d H:i:s")." set 5 stars \n\r");
		if ($orders) {
			foreach ($orders as $key => $order) {
				$order_id = $order->id;
				//是否已存在评论记录
				$sql = "SELECT count(*) FROM jf_order_comment WHERE order_id='{$order_id}'";
				$num = intval($this->db->get_var($sql));
				if ($num > 0) {
					$sql = "UPDATE jf_order_comment SET stars='5', add_time='".time()."', status='1' 
							WHERE order_id='{$order_id}'";
					$this->db->query($sql);
				} else {
					$sql = "INSERT INTO jf_order_comment(member_id, order_id, stars, add_time, status) 
						VALUES('{$order->member_id}', '{$order_id}', '5', '".time()."', '1')";
					$this->db->query($sql);
				}
				//echo $sql."<br>";
				//改变订单状态
				$sql = "UPDATE jf_order SET status='5' WHERE id='{$order_id}'";
				$this->db->query($sql);
				//计算美容师的星级
				if ($order->beautician_id) {
					$beautician_id = intval($order->beautician_id);
					$sql = "SELECT sum(stars)/count(stars) FROM jf_order_comment c INNER JOIN im_order o 
						WHERE c.order_id=o.id AND o.beautician_id='{$beautician_id}' AND c.status=1";
					//echo $sql;
					$stars = floatval($this->db->get_var($sql));
					//echo $stars;
					//$stars = "3.25";
					$arr = explode(".", strval($stars));
					if (count($arr)==2) {
						$num = floatval("0.".$arr[1]);
						if ($num < 0.25) {
							$stars = intval($arr[0]);
						} elseif ($num > 0.75) {
							$stars = intval($arr[0])+1;
						} else {
							$stars = floatval($arr[0].".5");
						}
					}
					$stars = floatval($stars);
					//$stars = round($stars, 1);
					//echo $stars;
					$sql = "UPDATE jf_beautician SET stars='{$stars}', comments=comments+1 WHERE id='{$beautician_id}'";
					$this->db->query($sql);
					//echo $sql."<br>";
				}
				fwrite($log, "{$order->id} ");
				//exit;
			}
			fclose($log);
		}
	}

	//使优惠券失效，每天需要处理一次的作业
	//过期的现金券状态：0
	public function unvalid_cash_coupon() {
		$now = time();
		$sql = "UPDATE jf_member_cash_coupon SET status='0' WHERE status='1' AND expiring_time<'{$now}'";
		//echo $sql;
		//exit;
		$this->db->query($sql);
		$log = fopen(ROOT_PATH."/temp/cron.log", 'a+');
		fwrite($log, date("Y-m-d H:i:s")." unvalid_cash_coupon \n\r");
		fclose($log);
	}

	//提前一个小时发信息给门店、美容师、用户，取1~2个小时区间的订单
	public function order_prepare() {
		$begin = date("Y-m-d H:i", time()+1800);
		$end = date("Y-m-d H:i", time()+7200);
		$sql = "SELECT o.*, s.name as shop_name, s.sms_mobile as shop_mobile, 
			b.mobile as beautician_mobile, b.name as beautician_name 
			FROM jf_order o LEFT JOIN im_beautyshop s ON o.shop_id=s.id 
			LEFT JOIN jf_beautician b ON o.beautician_id=b.id 
			WHERE service_time>='{$begin}' and service_time<='{$end}' and o.status IN (1,2)";
		$orders = $this->db->get_results($sql);
		//echo strtotime("2016-02-26 13:49");
		//echo $sql;
		//print_r($orders);
		//exit;
		if ($orders) {
			foreach ($orders as $key => $order) {
				//发信息给用户
				if ($order->sms_notify_customer_before_1hour==0 && $order->mobile) {
					//【i美网】好期待！您预约的服务时间{1}就快要到了~{2}的美容师已经准备好为您服务了，期待您的光临！
					//$message = array($order->service_time, $order->shop_name);
                    $content =array("time"=>$order->service_time,"name"=>$order->shop_name);

					send_sms($order->mobile, $content, "SMS_7390193");
				}
				//发信息给门店
				//code...
				//发信息给美容师
				//code...
				$sql = "UPDATE jf_order SET sms_notify_customer_before_1hour='1', 
					sms_notify_beautyshop_before_1hour='1', sms_notify_beautician_before_1hour='1' 
					WHERE id='{$order->id}'";
				$this->db->query($sql);
				$log = fopen(ROOT_PATH."/temp/cron.log", 'a+');
				fwrite($log, date("Y-m-d H:i:s")." send sms to member: order_id {$order->id} \n\r");
				fclose($log);
			}
		}
		$sql = "UPDATE jf_order SET member_id='-1' WHERE member_id='0'";
		$this->db->query($sql);
	}	
}
